﻿using BLL.Base;
using CommandService;
using Entity;
using Entity.DbEntity;
using Entity.ViewEntity;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace BLL.Basis
{
    public class bl_RepairPrice : PubMethod
    {
        Commands cmd;
        LoginInfo userLogin;
        List<OperateModel> OperList;
        OperateModel oper;
        OperateModel operModel;
        ReturnResult rsl;
        public bl_RepairPrice()
        {
            cmd = new Commands();
            userLogin = new LoginInfo();
        }
        /// <summary>
        /// 维修价格数据表
        /// </summary>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <param name="condition"></param>
        /// <param name="pageCount"></param>
        /// <returns></returns>
        public ReturnResult getRepairPriceList(
            string pageIndex,
            string pageSize,
            string condition,
            ref string pageCount
            )
        {

            rsl = new ReturnResult();
            try
            {
                if (string.IsNullOrWhiteSpace(pageIndex))
                {
                    pageIndex = "1";
                }
                if (string.IsNullOrWhiteSpace(pageSize))
                {
                    pageSize = Tools.ConfigReader.Reader["PageSize"];
                }
                StringBuilder sb = new StringBuilder();
                sb = CreateSelectCondition(sb, condition);

                var list = cmd.PageList<ViewRepairPrice>("V_RepairPrice", pageIndex, pageSize, sb.ToString(), "id desc", ref pageCount);
                rsl.Data = list;
                rsl.Result = true;
            }
            catch (Exception ex)
            {
                rsl.Message = ex.Message;
            }
            return rsl;
        }

        /// <summary>
        /// 保存维修价格数据
        /// </summary>
        /// <param name="jsonStr"></param>
        /// <returns></returns>
        public ReturnResult saveRepairPrice(string jsonStr)
        {
            rsl = new ReturnResult();
            try
            {
                if (string.IsNullOrWhiteSpace(jsonStr))
                {
                    rsl.Message = "提交数据为空";
                    return rsl;
                }
                var json = JsonConvert.DeserializeObject<JObject>(jsonStr);

                if (string.IsNullOrWhiteSpace(json["proCodes"].ToString()))
                {
                    rsl.Message = "存货编码不允许为空";
                    return rsl;
                }

                OperList = new List<OperateModel>();
                DbRepairPrice repairPrice;

                var qTable = cmd.GetDataTable("select * from fun_RepairPrice_info where isdel=0");

                var arr = json["proCodes"].ToString().Split(',');
                foreach (string str in arr)
                {
                    if (!string.IsNullOrWhiteSpace(str))
                    {
                        //var existedId = 0;

                        var dr = qTable.Select("proCode='" + str+"'");
                        if (dr.Length > 0)
                        {
                            //existedId = (int)dr[0]["id"];

                            rsl.Message = "维修价格已存在，无法添加新条目";
                            return rsl;
                        }

                        repairPrice = new DbRepairPrice();

                        //warranty.id = existedId;

                        repairPrice.proCode = str;
                        repairPrice.rMoney = json["value"].ToString();
                        repairPrice.createtime = getNow();
                        repairPrice.createuser = userLogin.UserInfo().userCode;
                        repairPrice.status = "Y";
                        repairPrice.isDel = 0;

                        oper = new OperateModel();
                        oper.Data = repairPrice;
                        oper.Types = OperateModel.OperateType.Add;
                        OperList.Add(oper);
                    }

                }

                if (OperList.Count > 0)
                {
                    rsl = cmd.MoreUpdate(OperList);
                }
            }
            catch (Exception ex)
            {
                rsl.Message = ex.Message;
            }
            return rsl;
        }

        /// <summary>
        /// 删除维修价格
        /// </summary>
        /// <param name="jsonStr"></param>
        /// <returns></returns>
        public ReturnResult DelRepairPrice(string jsonStr)
        {
            rsl = new ReturnResult();
            try
            {
                OperList = new List<OperateModel>();

                var repairPrice = new DbRepairPrice
                {
                    delDate = getNow(),
                    delUser = userLogin.UserInfo().userCode,
                    id = int.Parse(jsonStr),
                    isDel = 1,
                };

                oper = new OperateModel();
                oper.Data = repairPrice;
                oper.Types = OperateModel.OperateType.Edit;
                oper.Conditions = "id";
                OperList.Add(oper);

                if (OperList.Count > 0)
                {
                    rsl = cmd.MoreUpdate(OperList);
                }
            }
            catch (Exception ex)
            {
                rsl.Message = ex.Message;
            }
            return rsl;
        }
    }
}
